#pragma once
#include "CDirMonitorData.h"

#define DEFAULT_WATCH_PARAMS		FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE

class CScanThread
{
protected:
  bool m_bContinue;
  CDirMonitorData &m_data; // shared
  CDirMonitorData m_localdata;    //local copy
public:
 // boost::mutex _objectMutex;

  CScanThread(CDirMonitorData &data):
      m_bContinue(true)
     ,m_data(data)
  {}
  
  void operator ()()
  {
    //make safe copy
    {
     // boost::mutex::scoped_lock objectLock (_objectMutex);
      m_localdata = m_data;

    }
    HANDLE hDir = CreateFile( m_localdata.m_strDir.c_str(), // pointer to the file name
      FILE_LIST_DIRECTORY,                // access (read/write) mode
      FILE_SHARE_READ|FILE_SHARE_DELETE,  // share mode
      NULL,                               // security descriptor
      OPEN_EXISTING,                      // how to create
      FILE_FLAG_BACKUP_SEMANTICS,         // file attributes
      NULL                                // file with attributes to copy
    );

  FILE_NOTIFY_INFORMATION Buffer[1024];
  DWORD BytesReturned;
  while( ReadDirectoryChangesW(
                                hDir,                                  // handle to directory
                                &Buffer,                                    // read results buffer
                                sizeof(Buffer),                                // length of buffer
                                TRUE,                                 // monitoring option
                               // FILE_NOTIFY_CHANGE_SECURITY|
                                FILE_NOTIFY_CHANGE_CREATION|
                                //FILE_NOTIFY_CHANGE_LAST_ACCESS|
                                FILE_NOTIFY_CHANGE_LAST_WRITE|
                                FILE_NOTIFY_CHANGE_SIZE|
                             //   FILE_NOTIFY_CHANGE_ATTRIBUTES|
                                FILE_NOTIFY_CHANGE_DIR_NAME|
                                FILE_NOTIFY_CHANGE_FILE_NAME,             // filter conditions
                                &BytesReturned,              // bytes returned
                                NULL,                          // overlapped buffer
                                NULL// completion routine
                                ))
    {
   
    switch(Buffer[0].Action)
    {
      case FILE_ACTION_ADDED: 
      {
        std::cerr << "The file was added to the directory" << std::endl; break; 
      }
      case FILE_ACTION_REMOVED: 
      {
        std::cerr << "The file was removed from the directory" << std::endl; break; 
      }
      case FILE_ACTION_MODIFIED: 
      {
       std::cerr << "The file was modified. This can be a change in the time stamp or attributes." << std::endl; break; 
      }
      case FILE_ACTION_RENAMED_OLD_NAME:
      {
        std::cerr << "The file was renamed and this is the old name." << std::endl; break; 
      }
      case FILE_ACTION_RENAMED_NEW_NAME:
      {
        std::cerr << "The file was renamed and this is the new name." << std::endl; break;
      }
    }
      
    int i=0;
    do
      {
/*      m_Sec.Lock();
      int item = pList1->InsertItem(pList1->GetItemCount(), CString(Buffer[i].FileName).Left(Buffer[i].FileNameLength / 2) + " - " + helper_txt );
		  pList1->SetItemText(item, 1, tm.Format("%Y/%m/%d/ - %H:%M:%S"));
      i++;
      m_Sec.Unlock();*/
        TCHAR szFileName[MAX_PATH] = {0};
        WideCharToMultiByte(CP_ACP, NULL, Buffer[i].FileName, Buffer[i].FileNameLength, szFileName, sizeof(szFileName) , NULL, NULL);
        std::cerr << szFileName << " " << std::endl;
        ++i;
      }
    while (!Buffer[i].NextEntryOffset);
    memset(Buffer, 0, sizeof(Buffer));
    }
	}
	


};


